﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Effects;
using D3crft.Windows.UserControls;

namespace D3crft.Windows {

    public partial class wNewCharacter : Window {

        public delegate void delUniqCharComboHandler(string sCharName, string sCharClass);

        public event delUniqCharComboHandler evNewNameClassComboEntered;
        public event delUniqCharComboHandler evCreateClicked;

        ucTextBox oName;
        ucComboBoxAdvanced oClass;
        ucButtonAdvanced oCreate;
        ucButtonAdvanced oCancel;

        BlurEffect oBlur;

        DispatcherTimer oTimer;

        public wNewCharacter() {
            InitializeComponent();

            oName = FindName("Name") as ucTextBox;
            oClass = FindName("Class") as ucComboBoxAdvanced;
            oCreate = FindName("Create") as ucButtonAdvanced;
            oCancel = FindName("Cancel") as ucButtonAdvanced;

            oTimer = new DispatcherTimer();
            oTimer.Tick += new EventHandler(ev_oTimer_Tick);
            oTimer.Interval = TimeSpan.FromMilliseconds(25.0);

            oBlur = FindName("Blur") as BlurEffect;

            oName.evTextChanged += new TextChangedEventHandler(ev_oName_TextChanged);
            oName.evKeyDown += new KeyEventHandler(ev_oName_KeyDown);
            oClass.evTextChanged += new TextChangedEventHandler(ev_oClass_TextChanged);
            oCreate.evClicked += new EventHandler(ev_oCreate_Clicked);

            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(ev_Object_VisibleChanged);
            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);

            do_PopulateComboBox();
        }

        private void do_PopulateComboBox() {
            oClass.AddItem("Barbarian");
            oClass.AddItem("Crusader");
            oClass.AddItem("Demon Hunter");
            oClass.AddItem("Monk");
            oClass.AddItem("Witch Doctor");
            oClass.AddItem("Wizard");
        }


        public void AllowCreate() {
            oCreate.Enabled = true;
        }

        public void DenyCreate() {
            oCreate.Enabled = false;
        }

        private void ev_Object_VisibleChanged(object sender, DependencyPropertyChangedEventArgs ea) {
            if (this.IsVisible) {
                oBlur.Radius = 30.0;
                oTimer.Start();
            }
        }

        private void ev_Object_Loaded(object sender, EventArgs ea) {
            oName.Focus();
        }

        private void ev_oName_TextChanged(object sender, TextChangedEventArgs ea) {
            if ((oClass.IsValidItemSelected()) && (oName.Text != "")) {
                if (evNewNameClassComboEntered != null) evNewNameClassComboEntered(oName.Text, oClass.Text);
            }
            else {
                DenyCreate();
            }
        }

        private void ev_oName_KeyDown(object sender, KeyEventArgs ea) {
            if (ea.Key == Key.Enter) {
                if (oCreate.Enabled) {
                    ev_oCreate_Clicked(sender, ea);
                }
            }
        }

        private void ev_oClass_TextChanged(object sender, TextChangedEventArgs ea) {
            if ((oClass.IsValidItemSelected()) && (oName.Text != "")) {
                if (evNewNameClassComboEntered != null) evNewNameClassComboEntered(oName.Text, oClass.Text);
            }
            else {
                DenyCreate();
            }
        }

        private void ev_oCreate_Clicked(object sender, EventArgs ea) {
            if (evCreateClicked != null) evCreateClicked(oName.Text, oClass.Text);
        }

        private void ev_oTimer_Tick(object sender, EventArgs ea) {
            oBlur.Radius -= 2.5;
            if (oBlur.Radius == 0.0) {
                oTimer.Stop();
            }
        }

        private void ev_oCancel_Clicked(object sender, EventArgs ea) {
            this.Close();
        }
    }
}
